<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="./bootstrap.css">
  <link rel="stylesheet" href="./markdown.css">
</head>
<body>
  <div id="app">
    <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script src="./axios.min.js"></script>
  <script>
    const nodeDetail = {
      template: `
        <div class="container">
          <div class="row">
            <div class="col-xs-8">
              <nodeDetailHeader :headerInfo = "topicHeader"></nodeDetailHeader>
              <nodeContent :content="topic.content"></nodeContent>
              <nodeComment :replies="topic.replies" :reply_count="topic.reply_count"></nodeComment>
            </div>
            <div class="col-xs-4">
              <nodeAuthorInfo :name="authorInfo.loginname" :avatar="authorInfo.avatar_url" :score="authorInfo.score"></nodeAuthorInfo>
              <nodeAuthorTopics :topics="authorInfo.recent_topics"></nodeAuthorTopics>
            </div>
          </div>
        </div>
      `,
      data () {
        return {
          topic: {},
          authorInfo: {}
        }
      },
      computed: {
        topicHeader () {
          const {title, create_at, visit_count, tab, author} = this.topic
          // const {loginname} = author
          // console.log(title)
          return {title, create_at, visit_count, tab, author}
        }
      },
      created () {
        axios.get('https://cnodejs.org/api/v1/topic/5433d5e4e737cbe96dcef312').then(res => {
          this.topic = res.data.data
          // 利用名字进行第二次请求
          return axios.get('https://cnodejs.org/api/v1/user/' + res.data.data.author.loginname)
        }).then(res => {
          this.authorInfo = res.data.data
        })


      }
    }

    const nodeDetailHeader = {
      template: `
        <div class="header" v-if="headerInfo.title">
          <h2>{{headerInfo.title}}</h2>
          <div>
            <span>发布于{{headerInfo.create_at}}</span>
            <span>作者{{headerInfo.author.loginname}}</span>
            <span>{{headerInfo.visit_count}}次浏览</span>
            <span>来自{{headerInfo.tab}}</span>
          </div>
        </div>
      `,

      props: {
        headerInfo: Object
      }
    }
    
    const nodeContent = {
      template: ` 
        <div class="markdown-body" v-html="content"></div>
      `,
      props: {
        content: String
      }
    }

    const nodeComment = {
      template: `
        <div class="panel panel-default" v-if="reply_count">
          <div class="panel-heading">{{reply_count}}回复</div>
          <div class="panel-body">
            <ul class="media-list">
              <li class="media" v-for="reply in replies">
                <div class="media-left">
                  <a href="#">
                    <img width="30" height="30" class="media-object" :src="reply.author.avatar_url" alt="...">
                  </a>
                </div>
                <div class="media-body">
                  <h4 class="media-heading">{{reply.author.loginname}}</h4>
                  <div v-html="reply.content"></div>
                </div>
              </li>
            </ul>
          </div>
          
          
        </div>
      `,
      props: {
        replies: Array,
        reply_count: {
          type: Number,
          default: 0
        }
      }
    }
    

    const nodeAuthorInfo = {
      template: `
        <div class="panel panel-default">
          <div class="panel-heading">作者</div>
          <div class="panel-body">
            <img :src="avatar">
            {{name}}
            <br>
            积分：{{score}}
          </div>
        </div>
      `,
      props: {  
        name: String,
        score: Number,
        avatar: String
      },
      created () {
       

      }
    }

   
    const nodeAuthorTopics = {
      template: `
        <div class="panel panel-default">
          <div class="panel-heading">作者的其他文章</div>
          <ul class="list-group">
            <li class="list-group-item" v-for="topic in topics">{{topic.title}}</li>
          </ul>
        </div>
      `,
      props: {
        topics: Array
      }
    }
    
    Vue.component('nodeDetailHeader', nodeDetailHeader)
    Vue.component('nodeContent', nodeContent)
    Vue.component('nodeComment', nodeComment)
    Vue.component('nodeAuthorInfo', nodeAuthorInfo)
    Vue.component('nodeAuthorTopics', nodeAuthorTopics)

    const routes = [
      {
        path: '/topic/:id',
        name: 'topic',
        component: nodeDetail
      }
    ]

    const router = new VueRouter({
      routes
    })

    const app = new Vue({
      el: '#app',
      router
    })
  
  </script>
</body>
</html>